
/* Scheduler includes. */
#include "./freeRTOS/include/FreeRTOS.h"
#include "./freeRTOS/include/task.h"
#include "./freeRTOS/include/queue.h"


/* Library includes. */
#include "./stm32files/inc/stm32f10x_it.h"
#include "./stm32files/inc/stm32f10x_rtc.h"
#include "./stm32files/inc/stm32f10x_rcc.h"
#include "./stm32files/inc/misc.h"

#include "./projectItself/timer_hal.h"
#include "pwm_data.h"

#include "utilities.h"

void * _sbrk(int incr) 
{
	return 0;
}
int sprintf ( char * str, const char * format, ... )
{
return 0;
}

void strcat( char * dest, const char * source )
{
}

int strlen( char * name )
{
 return 0;
}

void 	memset (void *b, int c, size_t len)
{
}
/* The time between cycles of the 'check' task - which depends on whether the
check task has detected an error or not. */
#define mainCHECK_DELAY_NO_ERROR			( ( portTickType ) 5000 / portTICK_RATE_MS )
#define mainCHECK_DELAY_ERROR				( ( portTickType ) 500 / portTICK_RATE_MS )

/* The LED controlled by the 'check' task. */
#define mainCHECK_LED						( 3 )

/* Task priorities. */
#define mainUsbTaskPriority				( tskIDLE_PRIORITY + 1 )
#define mainBlinkTaskPriority				( tskIDLE_PRIORITY + 2 )

/*-----------------------------------------------------------*/




static void prvBlinkLedTask( void *pvParameters );

static void prvSetupHardware( void );

xTaskHandle usbTask;

int main( void )
{

	/* Set up the clocks and memory interface. */
	prvSetupHardware();


	/* Create the 'check' task, which is also defined within this file. */
xTaskCreate( prvServiceUsbTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainUsbTaskPriority, &usbTask );
	/* Create the 'echo' task, which is also defined within this file. */
	xTaskCreate( prvBlinkLedTask, ( signed char * ) "Echo", configMINIMAL_STACK_SIZE, NULL, mainBlinkTaskPriority, NULL );

    /* Start the scheduler. */
	vTaskStartScheduler();

    /* Will only get here if there was insufficient memory to create the idle
    task.  The idle task is created within vTaskStartScheduler(). */
	for( ;; );
}


/* Described at the top of this file. */
static void prvBlinkLedTask( void *pvParameters )
{

	while(1){
			//		blink();
         vTaskDelay( 1000 );
	};
}

/*-----------------------------------------------------------*/

static void prvSetupHardware( void )
{
	// RCC system reset(for debug purpose). */
//	RCC_DeInit ();                        

  // Enable HSE. */
//	RCC_HSEConfig( RCC_HSE_ON );           
	
	// Wait till HSE is ready. */
//	while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
	
  // HCLK = SYSCLK. */
//	RCC_HCLKConfig( RCC_SYSCLK_Div1 );   

  // PCLK2  = HCLK. */
//	RCC_PCLK2Config( RCC_HCLK_Div1 );     

  // PCLK1  = HCLK/2. */
//	RCC_PCLK1Config( RCC_HCLK_Div2 );     

	// ADCCLK = PCLK2/4. */
	//RCC_ADCCLKConfig( RCC_PCLK2_Div4 );    
	
  // Flash 2 wait state. */
//	*( volatile unsigned long  * )0x40022000 = 0x01;           
	
	/* PLLCLK = 8MHz * 9 = 72 MHz */
	RCC_PLLConfig( RCC_PLLSource_HSI_Div2, RCC_PLLMul_12 );
	
  // Enable PLL. */
	RCC_PLLCmd( ENABLE );
	
	// Wait till PLL is ready. */
	while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
	
	// Select PLL as system clock source. */
	RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
	
	// Wait till PLL is used as system clock source. */
	while (RCC_GetSYSCLKSource() != 0x08);

	// Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
	RCC_APB2PeriphClockCmd(	RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
							| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );

	// Set the Vector Table base address at 0x08000000. */
	NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );

	NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );

	// Configure HCLK clock as SysTick clock source. */
	SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );


	// SPI2 Periph clock enable */
	//RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
	
	//initBlinkDiode();
	
	enablePwmOnTimer2();
  enablePwmOnTimer3();
	
	Set_USBClock();
	USB_Interrupts_Config();
	USB_Init();
	//zobaczyc  co bedie  jak  to  znikinie
	//Virtual_Com_Port_Reset();
	
/*	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);*/

	
}
/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
{
	/* This function will get called if a task overflows its stack.   If the
	parameters are corrupt then inspect pxCurrentTCB to find which was the
	offending task. */

	( void ) pxTask;
	( void ) pcTaskName;

	for( ;; );
}
/*-----------------------------------------------------------*/

void assert_failed( unsigned char *pucFile, unsigned long ulLine )
{
	for( ;; );
}
#ifdef  T

 // Mutex semaphores cannot be used before a call to // xSemaphoreCreateMutex(). The created mutex is returned. xSemaphore = xSemaphoreCreateMutex();

#endif
